package com.hk.yajin.function.members.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.members.activity.setting.ForgetPwdActivity;
import com.hk.yajin.function.members.fragment.LoginFragment;
import com.hk.yajin.function.members.fragment.RegisterFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginAndRegisterActivity extends BaseActivity {

    @BindView(R.id.ctv_login)
    CheckedTextView ctvLogin;
    @BindView(R.id.line_left)
    View lineLeft;
    @BindView(R.id.ctv_register)
    CheckedTextView ctvRegister;
    @BindView(R.id.line_right)
    View lineRight;
    @BindView(R.id.lly_container)
    LinearLayout llyContainer;
    @BindView(R.id.activity_login)
    LinearLayout activityLogin;
    @BindView(R.id.tv_forget)
    TextView tvForget;

    /*页面切换*/
    private LoginFragment loginFragment;
    private RegisterFragment registFragment;
    private FragmentManager fm;
    private FragmentTransaction ft;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        intiView();
    }

    private void intiView() {
        initToolBar(R.string.login);
        onViewClicked(lineLeft);
    }

    @OnClick({R.id.ctv_login, R.id.line_left, R.id.ctv_register, R.id.line_right, R.id.tv_forget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ctv_login:
            case R.id.line_left:
                if (loginFragment == null) {
                    loginFragment = new LoginFragment();
                }
                ctvLogin.setChecked(true);
                ctvRegister.setChecked(false);
                lineLeft.setVisibility(View.VISIBLE);
                lineRight.setVisibility(View.INVISIBLE);
                switchFragment(loginFragment);
                break;
            case R.id.ctv_register:
            case R.id.line_right:
                if (registFragment == null) {
                    registFragment = new RegisterFragment();
                }
                ctvRegister.setChecked(true);
                ctvLogin.setChecked(false);
                lineRight.setVisibility(View.VISIBLE);
                lineLeft.setVisibility(View.INVISIBLE);
                switchFragment(registFragment);
                break;
            case R.id.tv_forget:
                startActivity(ForgetPwdActivity.class);
                break;
        }
    }


    private void switchFragment(Fragment target) {
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.lly_container, target);
        ft.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
